blob: 05e11313a528bb3f2c87b367981e9c6250b52b6b [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"
24#include "rtc_base/basictypes.h"
25#include "rtc_base/criticalsection.h"
Niels Möller6daa2782018-01-23 10:37:42 +010026#include "media/base/audiosource.h"
27#include "media/base/mediachannel.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "pc/dtmfsender.h"
ossu7bb87ee2017-01-23 04:56:25 -080029
30namespace webrtc {
31
Steve Anton2d8609c2018-01-23 16:38:46 -080032class StatsCollector;
33
ossu7bb87ee2017-01-23 04:56:25 -080034// Internal interface used by PeerConnection.
35class RtpSenderInternal : public RtpSenderInterface {
36 public:
Steve Anton57858b32018-02-15 15:19:50 -080037 // Sets the underlying MediaEngine channel associated with this RtpSender.
38 // SetVoiceMediaChannel should be used for audio RtpSenders and
39 // SetVideoMediaChannel should be used for video RtpSenders. Must call the
40 // appropriate SetXxxMediaChannel(nullptr) before the media channel is
41 // destroyed.
42 virtual void SetVoiceMediaChannel(
43 cricket::VoiceMediaChannel* voice_media_channel) = 0;
44 virtual void SetVideoMediaChannel(
45 cricket::VideoMediaChannel* video_media_channel) = 0;
46
ossu7bb87ee2017-01-23 04:56:25 -080047 // Used to set the SSRC of the sender, once a local description has been set.
48 // If |ssrc| is 0, this indiates that the sender should disconnect from the
49 // underlying transport (this occurs if the sender isn't seen in a local
50 // description).
51 virtual void SetSsrc(uint32_t ssrc) = 0;
52
Steve Anton8ffb9c32017-08-31 15:45:38 -070053 // TODO(steveanton): With Unified Plan, a track/RTCRTPSender can be part of
54 // multiple streams (or no stream at all). Replace these singular methods with
55 // their corresponding plural methods.
56 // Until these are removed, RtpSenders must have exactly one stream.
ossu7bb87ee2017-01-23 04:56:25 -080057 virtual void set_stream_id(const std::string& stream_id) = 0;
58 virtual std::string stream_id() const = 0;
Steve Anton8ffb9c32017-08-31 15:45:38 -070059 virtual void set_stream_ids(const std::vector<std::string>& stream_ids) = 0;
ossu7bb87ee2017-01-23 04:56:25 -080060
61 virtual void Stop() = 0;
Steve Anton57858b32018-02-15 15:19:50 -080062
63 // Returns an ID that changes every time SetTrack() is called, but
64 // otherwise remains constant. Used to generate IDs for stats.
65 // The special value zero means that no track is attached.
66 virtual int AttachmentId() const = 0;
ossu7bb87ee2017-01-23 04:56:25 -080067};
68
69// LocalAudioSinkAdapter receives data callback as a sink to the local
70// AudioTrack, and passes the data to the sink of AudioSource.
71class LocalAudioSinkAdapter : public AudioTrackSinkInterface,
72 public cricket::AudioSource {
73 public:
74 LocalAudioSinkAdapter();
75 virtual ~LocalAudioSinkAdapter();
76
77 private:
78 // AudioSinkInterface implementation.
79 void OnData(const void* audio_data,
80 int bits_per_sample,
81 int sample_rate,
82 size_t number_of_channels,
83 size_t number_of_frames) override;
84
85 // cricket::AudioSource implementation.
86 void SetSink(cricket::AudioSource::Sink* sink) override;
87
88 cricket::AudioSource::Sink* sink_;
89 // Critical section protecting |sink_|.
90 rtc::CriticalSection lock_;
91};
92
deadbeef20cb0c12017-02-01 20:27:00 -080093class AudioRtpSender : public DtmfProviderInterface,
94 public ObserverInterface,
ossu7bb87ee2017-01-23 04:56:25 -080095 public rtc::RefCountedObject<RtpSenderInternal> {
96 public:
97 // StatsCollector provided so that Add/RemoveLocalAudioTrack can be called
98 // at the appropriate times.
ossu7bb87ee2017-01-23 04:56:25 -080099
Steve Anton02ee47c2018-01-10 16:26:06 -0800100 // Construct an AudioRtpSender with a null track, a single, randomly generated
Seth Hampson845e8782018-03-02 11:34:10 -0800101 // stream id, and a randomly generated ID.
Steve Anton47136dd2018-01-12 10:49:35 -0800102 AudioRtpSender(rtc::Thread* worker_thread, StatsCollector* stats);
ossu7bb87ee2017-01-23 04:56:25 -0800103
Seth Hampson845e8782018-03-02 11:34:10 -0800104 // Construct an AudioRtpSender with the given track and stream ids. The
Steve Anton02ee47c2018-01-10 16:26:06 -0800105 // sender ID will be set to the track's ID.
Steve Anton47136dd2018-01-12 10:49:35 -0800106 AudioRtpSender(rtc::Thread* worker_thread,
107 rtc::scoped_refptr<AudioTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800108 const std::vector<std::string>& stream_ids,
Steve Anton02ee47c2018-01-10 16:26:06 -0800109 StatsCollector* stats);
ossu7bb87ee2017-01-23 04:56:25 -0800110
111 virtual ~AudioRtpSender();
112
deadbeef20cb0c12017-02-01 20:27:00 -0800113 // DtmfSenderProvider implementation.
114 bool CanInsertDtmf() override;
115 bool InsertDtmf(int code, int duration) override;
116 sigslot::signal0<>* GetOnDestroyedSignal() override;
117
118 // ObserverInterface implementation.
ossu7bb87ee2017-01-23 04:56:25 -0800119 void OnChanged() override;
120
deadbeef20cb0c12017-02-01 20:27:00 -0800121 // RtpSenderInterface implementation.
ossu7bb87ee2017-01-23 04:56:25 -0800122 bool SetTrack(MediaStreamTrackInterface* track) override;
123 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
124 return track_;
125 }
126
127 uint32_t ssrc() const override { return ssrc_; }
128
129 cricket::MediaType media_type() const override {
130 return cricket::MEDIA_TYPE_AUDIO;
131 }
132
133 std::string id() const override { return id_; }
134
Steve Anton8ffb9c32017-08-31 15:45:38 -0700135 std::vector<std::string> stream_ids() const override { return stream_ids_; }
ossu7bb87ee2017-01-23 04:56:25 -0800136
137 RtpParameters GetParameters() const override;
Zach Steinba37b4b2018-01-23 15:02:36 -0800138 RTCError SetParameters(const RtpParameters& parameters) override;
ossu7bb87ee2017-01-23 04:56:25 -0800139
deadbeef20cb0c12017-02-01 20:27:00 -0800140 rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
141
ossu7bb87ee2017-01-23 04:56:25 -0800142 // RtpSenderInternal implementation.
143 void SetSsrc(uint32_t ssrc) override;
144
145 void set_stream_id(const std::string& stream_id) override {
Steve Anton8ffb9c32017-08-31 15:45:38 -0700146 stream_ids_ = {stream_id};
ossu7bb87ee2017-01-23 04:56:25 -0800147 }
Steve Anton8ffb9c32017-08-31 15:45:38 -0700148 std::string stream_id() const override { return stream_ids_[0]; }
149 void set_stream_ids(const std::vector<std::string>& stream_ids) override {
150 stream_ids_ = stream_ids;
151 }
ossu7bb87ee2017-01-23 04:56:25 -0800152
153 void Stop() override;
154
Harald Alvestrandc72af932018-01-11 17:18:19 +0100155 int AttachmentId() const override { return attachment_id_; }
156
Steve Anton57858b32018-02-15 15:19:50 -0800157 void SetVoiceMediaChannel(
158 cricket::VoiceMediaChannel* voice_media_channel) override {
159 media_channel_ = voice_media_channel;
160 }
161 void SetVideoMediaChannel(
162 cricket::VideoMediaChannel* video_media_channel) override {
163 RTC_NOTREACHED();
Steve Anton47136dd2018-01-12 10:49:35 -0800164 }
ossu7bb87ee2017-01-23 04:56:25 -0800165
166 private:
167 // TODO(nisse): Since SSRC == 0 is technically valid, figure out
168 // some other way to test if we have a valid SSRC.
169 bool can_send_track() const { return track_ && ssrc_; }
170 // Helper function to construct options for
171 // AudioProviderInterface::SetAudioSend.
172 void SetAudioSend();
173 // Helper function to call SetAudioSend with "stop sending" parameters.
174 void ClearAudioSend();
175
deadbeef20cb0c12017-02-01 20:27:00 -0800176 sigslot::signal0<> SignalDestroyed;
177
Steve Anton47136dd2018-01-12 10:49:35 -0800178 rtc::Thread* const worker_thread_;
Steve Anton02ee47c2018-01-10 16:26:06 -0800179 const std::string id_;
Steve Anton8ffb9c32017-08-31 15:45:38 -0700180 // TODO(steveanton): Until more Unified Plan work is done, this can only have
181 // exactly one element.
182 std::vector<std::string> stream_ids_;
Steve Anton47136dd2018-01-12 10:49:35 -0800183 cricket::VoiceMediaChannel* media_channel_ = nullptr;
ossu7bb87ee2017-01-23 04:56:25 -0800184 StatsCollector* stats_;
185 rtc::scoped_refptr<AudioTrackInterface> track_;
deadbeef20cb0c12017-02-01 20:27:00 -0800186 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender_proxy_;
ossu7bb87ee2017-01-23 04:56:25 -0800187 uint32_t ssrc_ = 0;
188 bool cached_track_enabled_ = false;
189 bool stopped_ = false;
190
191 // Used to pass the data callback from the |track_| to the other end of
192 // cricket::AudioSource.
193 std::unique_ptr<LocalAudioSinkAdapter> sink_adapter_;
Harald Alvestrandc72af932018-01-11 17:18:19 +0100194 int attachment_id_ = 0;
ossu7bb87ee2017-01-23 04:56:25 -0800195};
196
197class VideoRtpSender : public ObserverInterface,
198 public rtc::RefCountedObject<RtpSenderInternal> {
199 public:
Steve Anton02ee47c2018-01-10 16:26:06 -0800200 // Construct a VideoRtpSender with a null track, a single, randomly generated
Seth Hampson845e8782018-03-02 11:34:10 -0800201 // stream id, and a randomly generated ID.
Steve Anton47136dd2018-01-12 10:49:35 -0800202 explicit VideoRtpSender(rtc::Thread* worker_thread);
ossu7bb87ee2017-01-23 04:56:25 -0800203
Seth Hampson845e8782018-03-02 11:34:10 -0800204 // Construct a VideoRtpSender with the given track and stream ids. The
Steve Anton02ee47c2018-01-10 16:26:06 -0800205 // sender ID will be set to the track's ID.
Steve Anton47136dd2018-01-12 10:49:35 -0800206 VideoRtpSender(rtc::Thread* worker_thread,
207 rtc::scoped_refptr<VideoTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800208 const std::vector<std::string>& stream_ids);
ossu7bb87ee2017-01-23 04:56:25 -0800209
210 virtual ~VideoRtpSender();
211
212 // ObserverInterface implementation
213 void OnChanged() override;
214
215 // RtpSenderInterface implementation
216 bool SetTrack(MediaStreamTrackInterface* track) override;
217 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
218 return track_;
219 }
220
221 uint32_t ssrc() const override { return ssrc_; }
222
223 cricket::MediaType media_type() const override {
224 return cricket::MEDIA_TYPE_VIDEO;
225 }
226
227 std::string id() const override { return id_; }
228
Steve Anton8ffb9c32017-08-31 15:45:38 -0700229 std::vector<std::string> stream_ids() const override { return stream_ids_; }
ossu7bb87ee2017-01-23 04:56:25 -0800230
231 RtpParameters GetParameters() const override;
Zach Steinba37b4b2018-01-23 15:02:36 -0800232 RTCError SetParameters(const RtpParameters& parameters) override;
ossu7bb87ee2017-01-23 04:56:25 -0800233
deadbeef20cb0c12017-02-01 20:27:00 -0800234 rtc::scoped_refptr<DtmfSenderInterface> GetDtmfSender() const override;
235
ossu7bb87ee2017-01-23 04:56:25 -0800236 // RtpSenderInternal implementation.
237 void SetSsrc(uint32_t ssrc) override;
238
239 void set_stream_id(const std::string& stream_id) override {
Steve Anton8ffb9c32017-08-31 15:45:38 -0700240 stream_ids_ = {stream_id};
ossu7bb87ee2017-01-23 04:56:25 -0800241 }
Steve Anton8ffb9c32017-08-31 15:45:38 -0700242 std::string stream_id() const override { return stream_ids_[0]; }
243 void set_stream_ids(const std::vector<std::string>& stream_ids) override {
244 stream_ids_ = stream_ids;
245 }
ossu7bb87ee2017-01-23 04:56:25 -0800246
247 void Stop() override;
Harald Alvestrandc72af932018-01-11 17:18:19 +0100248 int AttachmentId() const override { return attachment_id_; }
ossu7bb87ee2017-01-23 04:56:25 -0800249
Steve Anton57858b32018-02-15 15:19:50 -0800250 void SetVoiceMediaChannel(
251 cricket::VoiceMediaChannel* voice_media_channel) override {
252 RTC_NOTREACHED();
253 }
254 void SetVideoMediaChannel(
255 cricket::VideoMediaChannel* video_media_channel) override {
256 media_channel_ = video_media_channel;
Steve Anton47136dd2018-01-12 10:49:35 -0800257 }
ossu7bb87ee2017-01-23 04:56:25 -0800258
259 private:
260 bool can_send_track() const { return track_ && ssrc_; }
261 // Helper function to construct options for
262 // VideoProviderInterface::SetVideoSend.
263 void SetVideoSend();
264 // Helper function to call SetVideoSend with "stop sending" parameters.
265 void ClearVideoSend();
266
Steve Anton47136dd2018-01-12 10:49:35 -0800267 rtc::Thread* worker_thread_;
Steve Anton02ee47c2018-01-10 16:26:06 -0800268 const std::string id_;
Steve Anton8ffb9c32017-08-31 15:45:38 -0700269 // TODO(steveanton): Until more Unified Plan work is done, this can only have
270 // exactly one element.
271 std::vector<std::string> stream_ids_;
Steve Anton47136dd2018-01-12 10:49:35 -0800272 cricket::VideoMediaChannel* media_channel_ = nullptr;
ossu7bb87ee2017-01-23 04:56:25 -0800273 rtc::scoped_refptr<VideoTrackInterface> track_;
274 uint32_t ssrc_ = 0;
275 bool cached_track_enabled_ = false;
276 VideoTrackInterface::ContentHint cached_track_content_hint_ =
277 VideoTrackInterface::ContentHint::kNone;
278 bool stopped_ = false;
Harald Alvestrandc72af932018-01-11 17:18:19 +0100279 int attachment_id_ = 0;
ossu7bb87ee2017-01-23 04:56:25 -0800280};
281
282} // namespace webrtc
283
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200284#endif // PC_RTPSENDER_H_